#ifndef CANCOMM_HPP
#define CANCOMM_HPP

#include "comm_interface.hpp"
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

class CanComm : public CommInterface {
public:
    explicit CanComm(const std::string& interface, unsigned int bitrate = 500000);
    ~CanComm();
    
    bool initialize() override;
    bool send(const std::vector<uint8_t>& data) override;
    std::vector<uint8_t> receive() override;
    void registerCallback(DataReceivedCallback callback) override;
    std::string getName() const override;
    bool isConnected() const override;
    void shutdown() override;
    
private:
    void readThreadFunc();
    void processReceivedData();
    
    std::string interface_;
    int socket_ = -1;
    std::atomic<bool> running_{false};
    
    std::thread readThread_;
    std::thread processThread_;
    std::mutex dataMutex_;
    std::condition_variable dataCond_;
    
    std::queue<std::vector<uint8_t>> receiveQueue_;
    
    DataReceivedCallback callback_;
    unsigned int bitrate_;
};

#endif // CANCOMM_HPP